﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace OA.Common
{
    public class Log
    {
        ///<summary>
        /// 写日志
        ///</summary>
        public static void WriteLog(string msg)
        {
            WriteLog("", msg);
        }
        ///<summary>
        /// 写日志
        ///</summary>
        public static void WriteLog(string logFile, string msg)
        {
            System.IO.StreamWriter sw=null;
            try
            {
                var logPath = Keys.LogPath;
                logPath += "\\" + DateTime.Now.ToString("yyyyMM") + "\\";
                if (System.IO.Directory.Exists(logPath) == false)
                {
                    System.IO.Directory.CreateDirectory(logPath);
                }
                var filename = logPath;
                if (!string.IsNullOrEmpty(logFile)) filename += logFile + "_";
                filename += DateTime.Now.ToString("yyyyMMdd") + ".Log";
                sw = System.IO.File.AppendText(filename);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg + "\r\n");
            }
            catch (Exception e)
            { }
            finally
            {
                if (sw != null) sw.Close();
            }
        }
        ///<summary>
        /// 写日志
        ///</summary>
        public static void WriteLog(LogFile logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }

        ///<summary>
        /// 异步写日志
        ///</summary>
        public static void WriteLogAsyn(string logFile, string msg)
        {
            new System.Threading.Thread(t =>
            {
                WriteLog(logFile, msg);
            }).Start();
        }
        
        ///<summary>
        /// 异步写日志
        ///</summary>
        public static void WriteLogAsyn(LogFile logFile, string msg)
        {
            WriteLogAsyn(logFile.ToString(), msg);
        }


        ///<summary>
        /// 异步写日志
        ///</summary>
        public static void WriteLogAsyn(string msg)
        {
            WriteLogAsyn("", msg);
        }
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Log,
        Trace,
        Warning,
        Error,
        SQL,
        Check
    }
}

